/*
 * CPUTime.cpp
 *
 *  Created on: Apr 5, 2011
 *      Author: skyang
 */

#include "CPUTime.h"
#include <iostream>
#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>


typedef struct timeval TimeVal, *TimeValPtr;
/* timeval is a struct with fields tv_sec for seconds and
                                   tv_usec for microseconds */

int GetTime (TimeVal &pTime)
{
  struct rusage usage;
  int status;

  if ((status= getrusage(RUSAGE_SELF,&usage))!=0)
    return (status);  /* return error code */

  pTime=usage.ru_utime; /* user time */

  return (status);
}

long GetTimeDifference (TimeVal &pStart, TimeVal &pStop)
{
  long usec, sec;
  std ::cout << "In Get Time Difference "<< std::endl;
  sec=pStop.tv_sec-pStart.tv_sec;    /* difference in seconds */
  usec=pStop.tv_usec-pStart.tv_usec; /* difference in microseconds */


  return ( sec*1000000l +  usec); /* result in microseconds */
}
/* How to Use:
int main()
{
  TimeVal startTime, endTime;
  long totalTime;


  assert(GetTime(startTime)==0);
  for (long reps=0; reps < 20000000l; reps++)
    // call your function here instead of the for-loop
    ;
  assert(GetTime(endTime)==0);
  totalTime=GetTimeDifference(startTime, endTime);
  cout << "Total time = " << totalTime << " microseconds." << endl;
}
*/

